﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Phone.Controls;
using System.Diagnostics;

namespace Code4WP7SDK
{
    public class MetroGridHelper
    {
        private static bool _isVisible;        //是否隐藏
        private static Color _color = AppColorHelper.GetAppAccentColor().Color;           //颜色
        private static double _opacity = 0.3;        //透明度
        private static Grid _grid;             //当前的grid
        private static List<Rectangle> _rects;  //方块数目

        public static bool IsVisible
        {
            get { return _isVisible; }
            set
            {
                _isVisible = value;
                UpdateGrid();
            }
        }

        public static Color Color
        {
            get { return _color; }
            set
            {
                _color = value;
                UpdateGrid();
            }
        }

        public static double Opacity
        {
            get { return _opacity; }
            set
            {
                _opacity = value;
                UpdateGrid();
            }
        }

        private static void UpdateGrid()
        {
            if (_rects != null)
            {
                //给矩形方块添加颜色
                var brush = new SolidColorBrush(_color);
                foreach (var rect in _rects)
                {
                    rect.Fill = brush;
                }
                //控制grid的显示
                _grid.Visibility = _isVisible ? Visibility.Visible : Visibility.Collapsed;
                _grid.Opacity = _opacity;
            }
            else
            {
                BuildGrid();
            }
        }

        /// <summary>
        /// 构建Grid
        /// </summary>
        private static void BuildGrid()
        {
            _rects = new List<Rectangle>();
            var frame = Application.Current.RootVisual as Frame;
            if (frame == null || VisualTreeHelper.GetChildrenCount(frame) == 0)
            {
                Deployment.Current.Dispatcher.BeginInvoke(BuildGrid);
                return;
            }
            var child = VisualTreeHelper.GetChild(frame, 0);
            var childAsBorder = child as Border;
            var childAsGrid = child as Grid;
            if (childAsBorder != null)
            {
                var content = childAsBorder.Child;
                if (content == null)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(BuildGrid);
                    return;
                }
                childAsBorder.Child = null;
                Deployment.Current.Dispatcher.BeginInvoke(() => 
                {
                    Grid newGrid = new Grid();
                    childAsBorder.Child = newGrid;
                    newGrid.Children.Add(content);
                    PrepareGrid(frame, newGrid);
                });
            }
            else if (childAsGrid != null)
            {
                PrepareGrid(frame, childAsGrid);
            }
            else
            {
                Debug.WriteLine("根节点必须是border或者是grid");
            }
        }

        private static void PrepareGrid(Frame frame, Grid parent)
        {
            var brush = new SolidColorBrush(_color);
            _grid = new Grid();
            _grid.IsHitTestVisible = false;

            // To support both orientations, unfortunately more visuals need to
            // be used. An alternate implementation would be to react to the
            // orientation change event and re-draw/remove squares.
            double width = frame.ActualWidth;
            double height = frame.ActualHeight;
            double max = Math.Max(width, height);

            for (int x = 24; x < max; x+=37)
            {
                for (int y = 24; y < max; y += 37)
                {
                    var rect = new Rectangle 
                    {
                        Width = 25,
                        Height = 25,
                        VerticalAlignment = VerticalAlignment.Top,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Margin = new Thickness(x,y,0,0),
                        IsHitTestVisible = false,
                        Fill = brush
                    };
                    _grid.Children.Add(rect);
                    _rects.Add(rect);
                }
            }

            _grid.Visibility = _isVisible ? Visibility.Visible : Visibility.Collapsed;
            _grid.Opacity = _opacity;
            _grid.CacheMode = new BitmapCache();

            parent.Children.Add(_grid);
        }
    }
}
